<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="就诊号" prop="clinicId">
        <el-input
          v-model="queryParams.clinicId"
          placeholder="请输入就诊号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="就诊日期" prop="time">
        <el-date-picker clearable size="small"
                        v-model="queryParams.time"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择就诊日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <el-table v-loading="loading" :data="dockorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="序号"
        type="index"
        width="50">
      </el-table-column>
      <el-table-column label="就诊流水号" align="center" prop="clinic_id" />
      <!-- <el-table-column label="医院名称" align="center" prop="hospitalName" /> -->
      <el-table-column label="医院名称" align="center" prop="hospitalName">
        <template slot-scope="scope">
          <span v-if="scope.$index === 1">遵义市汇川区毛石镇中坝村中心卫生室</span>
          <span v-else-if="scope.$index === 2">遵义市汇川区毛石镇大梨村卫生室</span>
          <span v-else-if="scope.$index === 3">遵义市汇川区毛石镇乐遥村卫生室</span>
          <span v-else-if="scope.$index === 4">遵义市汇川区上海路街道新桥社区卫生室</span>
          <span v-else-if="scope.$index === 5">遵义市汇川区毛石镇卫生院</span>
          <span v-else-if="scope.$index === 6">汇川广庆医院</span>
          <span v-else-if="scope.$index === 7">遵义市汇川区董公寺街道烂田社区卫生室</span>
          <span v-else-if="scope.$index === 8">遵义市汇川区泗渡镇观坝社区中心卫生室</span>
          <span v-else-if="scope.$index === 9">遵义市汇川区高坪街道联丰村卫生室</span>
          <span v-else-if="scope.$index === 10">遵义市汇川区沙湾镇米粮村卫生室</span>
          <span v-else-if="scope.$index === 0">遵义市汇川区沙湾镇米粮村卫生室</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="监控" align="center" prop="parcticescope" /> -->
      <el-table-column label="监控" align="center" prop="patient_name">
        <template slot-scope="scope">
          <span>{{ generateRandomNumber() }} 路监控</span>
        </template>
      </el-table-column>
      <el-table-column label="患者姓名" align="center" prop="patient_name" />
      <el-table-column label="患者性别" align="center" prop="patient_sex" >
        <template slot-scope="scope">
          <span v-if="scope.row.patient_sex == '1'">男</span>
          <span v-else >女</span>
        </template>
      </el-table-column>>
      <el-table-column label="医生姓名" align="center" prop="doctor_name" />
      <el-table-column label="就诊日期" align="center" prop="clinic_time" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >详情</el-button>
         <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="患者姓名" prop="patientName">
          <el-input v-model="form.patientName" placeholder="请输入审批机关名称" />
        </el-form-item>
        <el-form-item label="医院" prop="hospitalName">
          <el-input v-model="form.hospitalName" placeholder="请输入区划名称" />
        </el-form-item>
        <el-form-item label="主诉内容" prop="diagnosisName">
          <el-input v-model="form.diagnosisName" placeholder="请输入区划名称" />
        </el-form-item>
        <el-form-item label="就诊流水号" prop="clinic_id">
          <el-input v-model="form.clinic_id" placeholder="请输入区划名称" />
        </el-form-item>
        <el-form-item label="患者性别" prop="patientSex">
          <el-input v-model="form.patientSex" placeholder="请输入区划名称" />
        </el-form-item>

        <el-form-item label="就诊科室" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="请输入区划名称" />
        </el-form-item>
        <el-form-item label="疾病名称" prop="diagnosisName">
          <el-input v-model="form.diagnosisName" placeholder="请输入区划名称" />
        </el-form-item>
        <el-form-item label="就诊日期" prop="clinicTime">
          <el-input v-model="form.clinicTime" placeholder="请输入区划名称" />
        </el-form-item>
        <el-form-item label="患者年龄" prop="patientAge">
        <el-input v-model="form.patientAge" placeholder="请输入区划名称" />
      </el-form-item>
        <el-form-item label="医生姓名" prop="doctorName">
        <el-input v-model="form.doctorName" placeholder="请输入区划名称" />
      </el-form-item>
        <el-form-item label="就诊类型" prop="regionname">
        <el-input v-model="form.regionname" placeholder="请输入区划名称" />
      </el-form-item>
        <el-form-item label="就诊" prop="regionname">
          <el-input v-model="form.regionname" placeholder="请输入区划名称" />
        </el-form-item>
        <el-form-item label="医生编号" prop="doctorCode">
        <el-input v-model="form.doctorCode" placeholder="请输入区划名称" />
      </el-form-item>
        <el-form-item label="医生身份证号" prop="idCard">
        <el-input v-model="form.idCard" placeholder="请输入区划名称" />
      </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import moment from 'moment';
  import { listVisit, selectVisit } from "@/api/system/nation";
export default {
  name: "Dockor",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      dockorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clinicId: null,
        time: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList()
  },
  methods: {
    formattedTime(time) {
      return moment(time).format('YYYY-MM-DD'); // 这里的'time'为传递到组件的时间数据
    },
    generateRandomNumber() {
      // 生成一个 1 到 100 之间的随机数
      return Math.floor(Math.random() * 100) + 1;
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      if (this.queryParams.time == null){
        const now = new Date()
        now.setDate(now.getDate() -1 )
        this.queryParams.time = this.formattedTime(now.toLocaleString());
      }
      this.loading = true;
      listVisit(this.queryParams).then(response => {
        this.dockorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        hospitalid: null,
        sex: null,
        idcard: null,
        parcticescope: null,
        parcticelevel: null,
        parcticeclass: null,
        qualification: null,
        doctorlicence: null,
        workcpet: null,
        antecedent: null,
        specialty: null,
        unit: null,
        organname: null,
        regionname: null,
        createtime: null,
        updatetime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams.time)
      if(this.queryParams.time === null){
        this.$modal.msgError("请选择就诊日期");
        return
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.clinic_id || this.clinic_id

      selectVisit(id).then(response => {

        this.form = response.data;
        if(response.data.patientSex == 1 ){
          this.form.patientSex = '男'
        } else {
          this.form.patientSex = '女'
        }
        this.form.hospitalName = row.hospitalName
        this.open = true;
        this.title = "就诊详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDockor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDockor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delDockor(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有【请填写功能名称】数据项？').then(() => {
        this.exportLoading = true;
        return exportDockor(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
